package com.cobra.mall.domain.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cobra.mall.config.MallConst;
import com.cobra.mall.domain.order.entity.OrderEntity;
import com.cobra.mall.domain.order.entity.request.OrderRequest;
import com.cobra.mall.domain.order.service.inter.OrderService;
import com.cobra.mall.util.MallUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private KafkaTemplate<String,Object> kafkaTemplate;



    @PostMapping()
    public String createOrder(@RequestBody OrderRequest orderRequest) throws JsonProcessingException {
        OrderEntity orderEntity =new OrderEntity();
        orderEntity.setGoodsId(orderRequest.getGoodsId()).setGoodsNum(orderRequest.getGoodsNum()).setPayer(orderRequest.getPayer());
        kafkaTemplate.send(MallConst.KAFKA_TOPIC_ORDER_NAME, MallUtil.fromObj2Json(orderEntity));
        return "success";
    }
    @GetMapping("/{payer}")
    public List<OrderEntity> queryOrder(@PathVariable("payer") String payer){
        return orderService.list(new QueryWrapper<OrderEntity>().eq("payer",payer));
    }
}
